package com.zwb.service.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zwb.service.common.utils.ConversionUtil;
import com.zwb.service.dto.EmployeeSalaryDto;
import com.zwb.service.mapper.EmployeeMapper;
import com.zwb.service.pojo.Employee;
import com.zwb.service.service.IEmployeeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.RoundingMode;
import java.text.DecimalFormat;
import java.time.temporal.ChronoUnit;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author zwb
 * @since 2022-08-23
 */
@Service
public class EmployeeServiceImpl extends ServiceImpl<EmployeeMapper, Employee> implements IEmployeeService {

    @Autowired(required = false)
    private EmployeeMapper employeeMapper;

    /**
     * 获取员工分页对象
     *
     * @param current        当前页
     * @param size           每页条数
     * @param beginDateScope 开始时间
     * @param endDateScope   结束时间
     * @param employee       员工信息
     * @return
     */
    @Override
    public Page<Employee> getEmployeeByPage(Integer current, Integer size, Date beginDateScope, Date endDateScope, Employee employee) {
        Page<Employee> page = new Page<>(current, size);
        Page<Employee> employeePage = employeeMapper.getEmployeeByPage(page, beginDateScope, endDateScope, employee);
        return page;
    }

    /**
     * 获取最大的工号 id
     *
     * @return
     */
    @Override
    public Integer maxWorkID() {

        List<Map<String, Object>> maps = employeeMapper.selectMaps(new QueryWrapper<Employee>().select("max(workID) as workID"));
        return Integer.parseInt(maps.get(0).get("workID").toString()) + 1;
    }

    /**
     * 添加员工
     *
     * @param employee
     */
    @Override
    public boolean addEmployee(Employee employee) {
        Date beginDate = employee.getBeginDate();
        Date endContract = employee.getEndContract();
        // 合同期限 保留两位小数
        long days = ConversionUtil.dateToLocalDate(beginDate).until(
                ConversionUtil.dateToLocalDate(endContract), ChronoUnit.DAYS);
        DecimalFormat decimalFormat = new DecimalFormat("##.00");
        decimalFormat.setRoundingMode(RoundingMode.DOWN);
        employee.setContractTerm(Double.parseDouble(decimalFormat.format(days / 365.00)));
        return employeeMapper.insert(employee) == 1;
    }

    /**
     * 根据 ids 查询 员工数据
     *
     * @param ids
     * @return
     */
    @Override
    public List<Employee> getEmployeesByIn(List<Integer> ids) {
        return employeeMapper.getEmployeesByIn(ids);
    }

    /**
     * 获取 员工工资帐套 分页
     *
     * @return
     */

    @Override
    public Page<Employee> getEmployeeWithSalary(Integer currentPage, Integer size) {
        Page<Employee> page = new Page<>(currentPage, size);
        page.setOptimizeCountSql(false);
        employeeMapper.getEmployeeWithSalary(page);
        return page;
    }



}
